package com.wl.vcode.web;

import com.wl.bean.Result;
import com.wl.vcode.bean.VcodeProperties;
import com.wl.vcode.util.VerifyCodeUtils;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@RestController
@RequestMapping("vcode")
public class VcodeDo {
    VcodeProperties properties;

    @GetMapping("config")
    VcodeProperties properties() {
        return properties;
    }
    @RequestMapping(path = "image",produces = MediaType.IMAGE_PNG_VALUE)
    public void image(HttpServletResponse response, HttpSession session ) throws IOException {
        //生成验证码图片返回浏览器
        String vcode = VerifyCodeUtils.outputImage(response);
        session.setAttribute("vcode", vcode);
        //设置超时时间
        long time = System.currentTimeMillis();
        time += properties().getTimeout() * 1000;
        session.setAttribute("vcode-time", time);
    }
    //TODO 定义发送验证码邮件的方法，提供给未来的控制器方法调用
    public Result email(String email,String msgName){
        //构建短信，发送
        return null;
    }
}
